package distributedbank.server.messages;

import distributedbank.server.Account;
import distributedbank.server.BankBranch;

/**
 * Represents a request to deposit a certain amount of money to the given
 * account. This type of request is only sent as part of a Transfer.
 */
public class TransferDeposit
        extends Transaction {
    public static final long serialVersionUID = 1;

    private final int amount; // amount to transfer in cents

    private final int originBranchId;

    private final int originAccountId;

    /**
     * Note: the transactionId must be globally unique!
     */
    public TransferDeposit(int branchId, int accountId, int transactionId,
            int fromPort, int amount, int originBranchId, int originAccountId) {
        super(branchId, accountId, transactionId, fromPort);
        this.amount = amount;
        this.originBranchId = originBranchId;
        this.originAccountId = originAccountId;
    }

    @Override
    public Acknowledgment runRequestSilent(BankBranch bankBranch) {

        Account account = bankBranch.getAccount(this.accountId);

        System.out.println("Transfering to account " + this.accountId
                + " at branch " + this.branchId);
        account.deposit(this.amount, this.transactionId);

        return new Acknowledgment(this.transactionId);

    }
}
